<template>
  <div class="app-container">
    

    <!-- 添加或修改手术发生的不良事件对话框 -->
    <div>
      <el-form ref="badeventsurgeryRef" :model="form" :rules="rules" label-width="68px">
         <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="事件类型" prop="badId">
                <el-cascader
                  v-model="form.badId"
                  :options="badeventList"
                  :props="{
                    getCheckedNodes:false,
                    label:'badName',
                    value:'badId'
                  }"
                  @change="handleChange"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="发生时间" prop="badTime">
                <el-date-picker
                    v-model="form.badTime"
                    type="datetime"
                    value-format="YYYY-MM-DD HH:mm:ss"
                    placeholder="发生时间"
                  />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="发生地点" prop="place">
                <el-input v-model="form.place" placeholder="发生地点" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label-width="98px" label="第一发现人" prop="createBy">
                <el-input v-model="form.createBy" placeholder="第一发现人" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="严重等级" prop="badLevel">
                
                 <el-select v-model="form.badLevel" placeholder="请选择">
                    <el-option
                      v-for="item in bad_event_level"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
              </el-form-item>
            </el-col>
           
           
           
          </el-row>
       
       
            <el-form-item label="患者情况">
                <el-row>
                   <el-form-item label-width="40px" label="血氧" prop="bloodOxygen">
                     <div style="display:flex">
                       <el-input style="width:80px" v-model="form.bloodOxygen" placeholder="血氧">
                        
                        </el-input>
                        <span style="padding-left:5px">%</span>
                      </div>
                    </el-form-item>
                  <el-form-item label-width="50px" label="血压" prop="foldPressure">
                    
                    <div style="display:flex;">
                      <el-input style="width:86px" v-model="form.foldPressure" placeholder="收缩压" />
                       <span style="padding:0 5px">/</span>
                      <el-input style="width:86px" v-model="form.unfoldPressure" placeholder="舒张压" />
                      <span>mmHG</span>
                    </div>
                  </el-form-item>

                   <el-form-item label-width="50px" label="心率" prop="heartRate">
                    <div style="display:flex">
                      <el-input style="width:80px" v-model="form.heartRate" placeholder="心率" />
                      <span style="padding-left:5px">次/分</span>
                    </div>
                  </el-form-item>
                   <el-form-item label-width="50px" label="体温" prop="bodyTemperature">
                     <div style="display:flex">
                       <el-input style="width:60px" v-model="form.bodyTemperature" placeholder="体温" />
                       <span style="padding-left:5px">°C</span>
                     </div>
                    </el-form-item>   
                </el-row>
            </el-form-item>
           
          
       
       
       
       
       
        <el-form-item label="事件描述" prop="eventDesc">
          <el-input :rows="2" type="textarea" v-model="form.eventDesc" placeholder="事件描述" />
        </el-form-item>
        <el-form-item label="处置情况" prop="measure">
          <el-input :rows="2" type="textarea" v-model="form.measure" placeholder="处置情况" />
        </el-form-item>
        <el-form-item label="原因分析" prop="reason">
          <el-input :rows="2" type="textarea" v-model="form.reason" placeholder="原因分析" />
        </el-form-item>
        <el-form-item label="特殊说明" prop="specialRemark">
          <el-input :rows="2" type="textarea" v-model="form.specialRemark" placeholder="特殊说明" />
        </el-form-item>
      </el-form>
       <div style="margin-bottom:10px">
          <el-button style="width:100%" type="primary" @click="submitForm">保存</el-button>
        </div>
    </div>


     <el-table v-loading="loading" :data="badeventsurgeryList" @selection-change="handleSelectionChange">
      <el-table-column label="时间" align="center" prop="badTime" />
      <el-table-column label="事件" align="center" prop="badName" />
      <el-table-column label="严重等级" align="center" prop="badLevel" />
      <el-table-column label="血压" align="center" prop="foldPressure" />
      <el-table-column label="心率" align="center" prop="heartRate" />
      <el-table-column label="血氧" align="center" prop="bloodOxygen" />
      <el-table-column label="体温" align="center" prop="bodyTemperature" />
      <el-table-column label="事件描述" align="center" prop="eventDesc" />
      <el-table-column label="处置情况" align="center" prop="measure" />
      <el-table-column label="原因分析" align="center" prop="reason" />
      <el-table-column label="特殊说明" align="center" prop="specialRemark" />
      <el-table-column label="操作" align="center" class-name="small-padding" fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manage:badeventsurgery:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:badeventsurgery:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>

 
</template>

<script setup name="Badeventsurgery">
import { listBadeventsurgery, getBadeventsurgery, delBadeventsurgery, addBadeventsurgery, updateBadeventsurgery } from "@/api/manage/badeventsurgery";
import { listBadevent} from "@/api/manage/badevent";
const { proxy } = getCurrentInstance();
const { bad_event_level } = proxy.useDict('bad_event_level');
const badeventsurgeryList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
let badeventList=ref([]);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const props=defineProps({
  activeSurgery: {
    type: Object,
    required: true
  }
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    surgeryId: null,
    badTime: null,
    badId: null,
    badName: null,
    badLevel: null,
    foldPressure: null,
    unfoldPressure: null,
    heartRate: null,
    bloodOxygen: null,
    bodyTemperature: null,
    eventDesc: null,
    measure: null,
    reason: null,
    specialRemark: null,
    status: null,
  },
  rules: {
    badSurgeryId: [
      { required: true, message: "id不能为空", trigger: "blur" }
    ],
    surgeryId: [
      { required: true, message: "手术计划ID不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "0启用1禁用不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询手术发生的不良事件列表 */
function getList() {
  loading.value = true;
  listBadeventsurgery(queryParams.value).then(response => {
    badeventsurgeryList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    badSurgeryId: null,
    surgeryId: props.activeSurgery.surgeryId,
    badTime: null,
    badId: null,
    badName: null,
    badLevel: null,
    foldPressure: null,
    unfoldPressure: null,
    heartRate: null,
    bloodOxygen: null,
    bodyTemperature: null,
    eventDesc: null,
    measure: null,
    reason: null,
    specialRemark: null,
    status: null,
    delFlag: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("badeventsurgeryRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.badSurgeryId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加手术发生的不良事件";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _badSurgeryId = row.badSurgeryId || ids.value
  getBadeventsurgery(_badSurgeryId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改手术发生的不良事件";
  });
}

/** 提交按钮 */
function submitForm() {
  form.value.surgeryId=props.activeSurgery.surgeryId
  proxy.$refs["badeventsurgeryRef"].validate(valid => {
    
        let data=JSON.parse(JSON.stringify(form.value))
       
        data.badId=form.value.badId[1]?form.value.badId[1]:''
    if (valid) {
      if (form.value.badSurgeryId != null) {
       
        updateBadeventsurgery(data).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBadeventsurgery(data).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _badSurgeryIds = row.badSurgeryId || ids.value;
  proxy.$modal.confirm('是否确认删除手术发生的不良事件编号为"' + _badSurgeryIds + '"的数据项？').then(function() {
    return delBadeventsurgery(_badSurgeryIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/badeventsurgery/export', {
    ...queryParams.value
  }, `badeventsurgery_${new Date().getTime()}.xlsx`)
}

getList();

listBadevent({pageNum:1,pageSize:100}).then(response => {
  badeventList.value = proxy.handleTree(response.data, "badId", "parentId");
  console.log(badeventList.value)
});
</script>
